home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / TabSet.java < prev    next >
Text File  |  1998-06-30  |  4KB  |  147 lines

  1. /*
  2.  * @(#)TabSet.java    1.6 98/04/09
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc.  ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATION OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.text;
  22.  
  23. import java.io.Serializable;
  24.  
  25. /**
  26.  * A TabSet is comprised of many TabStops. It offers methods for locating the
  27.  * closest TabStop to a given position and finding all the potential TabStops.
  28.  * It is also immutable.
  29.  * <p>
  30.  * Warning: serialized objects of this class will not be compatible with
  31.  * future swing releases.  The current serialization support is appropriate 
  32.  * for short term storage or RMI between Swing1.0 applications.  It will
  33.  * not be possible to load serialized Swing1.0 objects with future releases
  34.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  35.  * baseline for the serialized form of Swing objects.
  36.  *
  37.  * @author  Scott Violet
  38.  * @version 1.6 04/09/98
  39.  */
  40. public class TabSet implements Serializable
  41. {
  42.     /** TabStops this TabSet contains. */
  43.     private TabStop[]              tabs;
  44.  
  45.     /**
  46.      * Creates and returns an instance of TabSet. The array of Tabs
  47.      * passed in must be sorted in ascending order.
  48.      */
  49.     public TabSet(TabStop[] tabs) {
  50.     // PENDING(sky): If this becomes a problem, make it sort.
  51.     if(tabs != null) {
  52.         int          tabCount = tabs.length;
  53.  
  54.         this.tabs = new TabStop[tabCount];
  55.         System.arraycopy(tabs, 0, this.tabs, 0, tabCount);
  56.     }
  57.     else
  58.         this.tabs = null;
  59.     }
  60.  
  61.     /**
  62.      * Returns the number of Tab instances the receiver contains.
  63.      */
  64.     public int getTabCount() {
  65.     return (tabs == null) ? 0 : tabs.length;
  66.     }
  67.  
  68.     /**
  69.      * Returns the TabStop at index <code>index</code>. This will throw an
  70.      * IllegalArgumentException if <code>index</code> is outside the range
  71.      * of tabs.
  72.      */
  73.     public TabStop getTab(int index) {
  74.     int          numTabs = getTabCount();
  75.  
  76.     if(index < 0 || index >= numTabs)
  77.         throw new IllegalArgumentException(index +
  78.                           " is outside the range of tabs");
  79.     return tabs[index];
  80.     }
  81.  
  82.     /**
  83.      * Returns the Tab instance after <code>location</code>. This will
  84.      * return null if there are no tabs after <code>location</code>.
  85.      */
  86.     public TabStop getTabAfter(float location) {
  87.     int     index = getTabIndexAfter(location);
  88.  
  89.     return (index == -1) ? null : tabs[index];
  90.     }
  91.  
  92.     /**
  93.      * @return the index of the TabStop <code>tab</code>, or -1 if
  94.      * <code>tab</code> is not contained in the receiver.
  95.      */
  96.     public int getTabIndex(TabStop tab) {
  97.     for(int counter = getTabCount() - 1; counter >= 0; counter--)
  98.         // should this use .equals?
  99.         if(getTab(counter) == tab)
  100.         return counter;
  101.     return -1;
  102.     }
  103.  
  104.     /**
  105.      * Returns the index of the Tab to be used after <code>location</code>.
  106.      * This will return -1 if there are no tabs after <code>location</code>.
  107.      */
  108.     public int getTabIndexAfter(float location) {
  109.     int     current, min, max;
  110.  
  111.     min = 0;
  112.     max = getTabCount();
  113.     while(min != max) {
  114.         current = (max - min) / 2 + min;
  115.         if(location > tabs[current].getPosition()) {
  116.         if(min == current)
  117.             min = max;
  118.         else
  119.             min = current;
  120.         }
  121.         else {
  122.         if(current == 0 || location > tabs[current - 1].getPosition())
  123.             return current;
  124.         max = current;
  125.         }
  126.     }
  127.     // no tabs after the passed in location.
  128.     return -1;
  129.     }
  130.  
  131.     /**
  132.      * Returns the string representation of the set of tabs.
  133.      */
  134.     public String toString() {
  135.     int            tabCount = getTabCount();
  136.     StringBuffer   buffer = new StringBuffer("[ ");
  137.  
  138.     for(int counter = 0; counter < tabCount; counter++) {
  139.         if(counter > 0)
  140.         buffer.append(" - ");
  141.         buffer.append(getTab(counter).toString());
  142.     }
  143.     buffer.append(" ]");
  144.     return buffer.toString();
  145.     }
  146. }
  147.